package com.peng.leetcode.tree;

/**
 * SumNumbers1
 * 129. 求根节点到叶节点数字之和
 * https://leetcode.cn/problems/sum-root-to-leaf-numbers/
 * <p>
 * Created on 2022/12/2
 *
 * @author lupeng
 */
public class SumNumbers1 {
    public int sumNumbers(TreeNode root) {
        if (root == null) {
            return 0;
        }
        dfs(root, 0);
        return ans;
    }

    int ans = 0;

    void dfs(TreeNode n, int val) {
        if (n == null) {
            return;
        }
        int currVal = val * 10 + n.val;
        if (n.left == null && n.right == null) {
            ans += currVal;
            return;
        }
        dfs(n.left, currVal);
        dfs(n.right, currVal);
    }
}
